const uuid = require('short-uuid')
const Core = require('@alicloud/pop-core');
const randomstring = require('randomstring')
const bcrypt = require('bcrypt')
const jwt =require('jsonwebtoken')

module.exports = {
  getUuid () {
    return uuid.generate()
  },
  randomCode () {
    return Math.floor(Math.random() * 90000 + 10000)
  },
  randomNickname () {
    return 'hg_' + randomstring.generate(10)
  },
  encryption (password) {
    const saltRounds = 10
    return bcrypt.hash(password, saltRounds)
  },
  
  sendCode (PhoneNumbers, code) {
    var client = new Core({
      accessKeyId: 'LTAI4GExoXbNkoRZqSFJYkWz',
      accessKeySecret: 'i8jrNC0C05b4UnCnXTHl4qsn3qcPI4',
      endpoint: 'https://dysmsapi.aliyuncs.com',
      apiVersion: '2017-05-25'
    });

    var params = {
      "RegionId": "cn-hangzhou",
      "PhoneNumbers": PhoneNumbers,
      "SignName": "大勋说",
      "TemplateCode": "SMS_171853298",
      "TemplateParam": "{ code: "+ code +" }"
    }

    var requestOption = {
      method: 'POST'
    };

    return new Promise((resolve, reject) => {
      client.request('SendSms', params, requestOption).then((result) => {
        console.log(JSON.stringify(result));
        resolve()
      }, (ex) => {
        console.log(ex);
        reject()
      })
    })

  },
  createToken(payload,secret,options){
    return jwt.sign(payload,secret,options)
  },
  validateToken (token, secret) {
    return new Promise((resolve, reject) => {
      jwt.verify(token, secret, (err, decoded) => {
        if (err) {
          reject()
        } else {
          resolve(decoded)
        }
      })
    })
  },
  checkEncryption (password, hash) {
    return bcrypt.compare(password, hash)
  },
}




//   sendCode (PhoneNumbers, code) {
//     var client = new Core({
//       accessKeyId: 'LTAI4GCKQxqeqqqcSjHt3RFY',
//       accessKeySecret: 'Zwearncvha3E8Ty1qG8x0cu3S6AmAW',
//       endpoint: 'https://dysmsapi.aliyuncs.com',
//       apiVersion: '2017-05-25'
//     });

//     var params = {
//       "RegionId": "cn-hangzhou",
//       "PhoneNumbers": PhoneNumbers,
//       "SignName": "阿杰学习网",
//       "TemplateCode": "SMS_205607674",
//       "TemplateParam": "{ code: "+ code +" }"
//     }

